#!/usr/bin/env python
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext import db
import cgi
import os
from google.appengine.ext.webapp import template
import datetime
import time
from dbmodel import Competition

class LoginHandler(webapp.RequestHandler):
  def post(self):
     usersComps = db.GqlQuery("SELECT * FROM Competition WHERE uemail = :1 and password = :2 order by date", cgi.escape(self.request.get('email')), cgi.escape(self.request.get('password')))
     comps = usersComps.fetch(100)
     path = os.path.join(os.path.dirname(__file__), 'usercomps.html')
     template_values = {'usercomps' : comps, 'email' : self.request.get('email'), 'password' : self.request.get('password')}
     self.response.out.write(template.render(path, template_values))

class AboutHandler(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'about.html')
    template_values = []
    self.response.out.write(template.render(path, template_values))

class DeveloperHandler(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'developer.html')
    template_values = []
    self.response.out.write(template.render(path, template_values))

class NewHandler(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'createnew.html')
    template_values = []
    self.response.out.write(template.render(path, template_values))

  def post(self):
    newC = Competition()
    newC.name = cgi.escape(self.request.get('txtName'))
    newC.organizer = cgi.escape(self.request.get('txtOrganizer'))
    datum = datetime.datetime.strptime(self.request.get('txtDate'),"%Y-%m-%d")
    newC.date = datum.date()
    newC.country = cgi.escape(self.request.get('selCountry'))
    newC.password = cgi.escape(self.request.get('txtPassword'))
    newC.uname = cgi.escape(self.request.get('txtUname'))
    newC.uemail = cgi.escape(self.request.get('txtEmail'))
    
    newC.put()

    path = os.path.join(os.path.dirname(__file__), 'createnew_done.html')
    template_values = []#["competitionname" : newC.name]
    self.response.out.write(template.render(path, template_values))

class MainHandler(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'index.html')

    upcoming = db.GqlQuery("SELECT * FROM Competition WHERE date > :1 order by date desc LIMIT 10", datetime.date.today())
    curent = db.GqlQuery("SELECT * FROM Competition WHERE date = :1", datetime.date.today())
    archive = db.GqlQuery("SELECT * FROM Competition WHERE date < :1", datetime.date.today())
   
    template_values = {
	'upcoming': upcoming.fetch(100),
	'curent' : curent.fetch(100),
	'archive' : archive.fetch(100)
	}
    self.response.out.write(template.render(path, template_values))


def main():
  application = webapp.WSGIApplication([('/', MainHandler),('/Login', LoginHandler),('/About', AboutHandler), ('/Developer',DeveloperHandler),('/CreateNew',NewHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
